function locationLink(address) {
	var gcg = new GClientGeocoder();
	gcg.getLatLng(address,  centerMap);
}
function centerMap (point)
{
	map.setZoom(10);
	map.setCenter(point);
}

function loadCalendarByAddress(calendarAddress) {
  var calendarUrl = 'http://www.google.com/calendar/feeds/' +
                    calendarAddress + 
                    '/public/full';
  loadCalendar(calendarUrl);
}

function loadCalendar(calendarUrl) {
  var service = new 
  google.gdata.calendar.CalendarService('gdata-js-client-samples-simple');
  var query = new google.gdata.calendar.CalendarEventQuery(calendarUrl);
  query.setOrderBy('starttime');
  query.setSortOrder('ascending');
  query.setFutureEvents(true);
  query.setSingleEvents(true);
  query.setMaxResults(100);
  //add "trip" to the query
  query.setParam('q','trip');
  service.getEventsFeed(query, listEvents, handleGDError);
}

function listEvents(feedRoot) {
  	var entries = feedRoot.feed.getEntries();
  	var eventDiv = document.getElementById('events');
  	if (eventDiv.childNodes.length > 0) {
   	 	eventDiv.removeChild(eventDiv.childNodes[0]);
  	}	  
  	/* create a new unordered list */
  	var ul = document.createElement('ul');
  	/* set the calendarTitle div with the name of the calendar */
  	document.getElementById('calendarTitle').innerHTML = 
    	feedRoot.feed.title.$t+"'s <a href = \"#\">Calendar</a>";
  	/* loop through each event in the feed */
  	var len = entries.length;
	var insert_this="";
  	for (var i = 0; i < len; i++) {
    		var entry = entries[i];
    		var title = entry.getTitle().getText();
		var location = entry.getLocations();
		var why = entry.getContent();
    		var startDateTime = null;
    		var startJSDate = null;
    		var times = entry.getTimes();
    		if (times.length > 0) {
      			startDateTime = times[0].getStartTime();
      			startJSDate = startDateTime.getDate();
    		}
		var dateString = (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate()+ " ";
    		if (!startDateTime.isDateOnly()) {
			if (startJSDate.getHours()<12) {
      				dateString += startJSDate.getHours() + ":" + padNumber(startJSDate.getMinutes()) + " AM";
			}
			else if (startJSDate.getHours()==12){
				dateString += startJSDate.getHours() + ":" + padNumber(startJSDate.getMinutes()) + " PM";
			}
			else {
				dateString += startJSDate.getHours()-12 + ":" + padNumber(startJSDate.getMinutes()) + " PM";
			}
    		}
		//insert_this+="<b>"+title+"</b><br>" + dateString + " <a href=\"#none\" onmouseover='locationLink(\"" + location[0].getValueString() + "\",\""+title+"\",\""+ why.getText() +"\")'>"+ location[0].getValueString() +"</a><br>";		
		//insert_this+="<b>"+title+"</b><br>" + dateString + " <a href=\"#none\" onclick='locationLink(\"San Francisco, CA\")'>"+ location[0].getValueString() +"</a><br>";
  		insert_this+="<b>"+title+"</b><br>" + dateString + " <a href=\"#\" onclick='locationLink(\""+location[0].getValueString()+"\")'>"+ location[0].getValueString() +"</a><br>";				
		}
	document.getElementById("events").innerHTML=insert_this;
}

function handleGDError(e) {
  document.getElementById('jsSourceFinal').setAttribute('style', 
      'display:none');
  if (e instanceof Error) {
    /* alert with the error line number, file and message */
    alert('Error at line ' + e.lineNumber +
          ' in ' + e.fileName + '\n' +
          'Message: ' + e.message);
    /* if available, output HTTP error code and status text */
    if (e.cause) {
      var status = e.cause.status;
      var statusText = e.cause.statusText;
      alert('Root cause: HTTP error ' + status + ' with status text of: ' + 
            statusText);
    }
  } else {
    alert(e.toString());
  }
}